{% extends "base.html" %}
{% block body %}

<div style="margin-top:5px;max-height:calc(90vh)" class="card shadow" id="searches">
    <div class="card-header bg-header border border-dark shadow text-white">
        <span style="display:inline-block">
            <h2 style="display:inline-block"><span class="operator">Search</span> the Operation...</h2>
        </span>
        <span style="display:inline-block; float:right">
            <span class="input-group">
                <select class="form-control" id="searchOperator" v-model="operator" :value="operator" style="position:relative;display:inline-block;color:white" v-if="option === 'params' || option === 'cmds' || option === 'comments'">
                    <option value="none">All Operators</option>
                    <option v-for="o in operators" :value="o.username">[[o.username]]</option>
                </select>
                <input v-if="option === 'file_browser'" class="form-control" type="text" v-model="file_browser_host" style="position:relative;display:inline-block;color:white" placeholder="Host Regex" size="30">
                <input v-if="option === 'file_browser'" class="form-control" type="text" v-model="file_browser_path" style="position:relative;display:inline-block;color:white" placeholder="Path/Filename Regex" size="50">
                <input v-if="option === 'file_browser'" class="form-control" type="text" v-model="file_browser_comment" style="position:relative;display:inline-block;color:white" placeholder="Comment Regex" size="50">
                <input v-if="option !== 'file_browser'" class="form-control" type="text" v-model="search_field" class="form-control" style="position:relative;display:inline-block;color:white" id="searchTextField" size="50" placeholder="Search with regex" height="7" @keyup.enter="search_button()">
                <select class="form-control" id="searchOption" v-model="option" :value="option" style="position:relative;display:inline-block;color:white" class="input-group-append">
                    <option value="output">Responses</option>
                    <option value="params">Command Parameters</option>
                    <option value="cmds">Commands</option>
                    <option value="comments">Comments</option>
                    <option value="file_browser">File Browsing</option>
                    <option value="event_log">Event Log Messages</option>
                </select>
                <span class="input-group-btn input-group-append" style="position:relative;display:inline-block">
                    <button class="btn btn-md btn{{config['outline-buttons']}}primary" @click="search_button()"><i class="fa fa-search"></i></button>
                </span>
            </span>
        </span>
    </div>
    <div style="overflow-y:auto;height:calc(80vh);padding:0;" class="bg-card-body border border-dark shadow">
        <div id="searchResults" >
            <div class="bg-header-dark border-top border-dark" v-for="task in responses" :key="'taskheader' + task.id" :id="'task' + task.id" style="margin:0px">
                <transition-group>
                <div class="card-header border-dark bg-header-dark" :key="'headera' + task.id" style="position:-webkit-sticky;position:static;top:0;padding-bottom:0;padding-top:0px;padding-left:0;white-space:nowrap;overflow-x:auto;max-width:100%">
                    <transition-group>
                     <button v-if="task.comment != '' && task.comment_visible" :key="'keya' + task.id" class="btn btn{{config['outline-buttons']}}danger btn-sm" @click="remove_comment(task)" style="padding:0px 4px;margin-left:2px;margin-top:2px;border:0"><i class="fas fa-trash-alt"></i></button>
                     <p v-if="task.comment != '' && task.comment_visible" :key="'keyb' + task.id" class="card-text" style="margin:0;display:inline-block">
                        <small class="text-muted">[[task.comment_operator]]'s comment:</small></p>
                     <pre v-if="task.comment != '' && task.comment_visible" :key="'keyc' + task.id" style="padding-left:27px">[[task.comment]]</pre>
                     <hr v-if="task.comment != '' && task.comment_visible" :key="'keyd' + task.id" style="margin-bottom: 0;margin-top:6px">

                     <div class="btn-group dropdown" :key="'statusdropdown' + task.id" style="display:inline-block">
                        <template v-if="task.opsec_pre_blocked === true && task.opsec_pre_bypassed === false">
                                 <button class="btn btn{{config['outline-buttons']}}warning btn-sm dropdown-toggle" style="border:0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="card-text">OPSEC BLOCKED (PRE)</span></button>
                             </template>
                             <template v-else-if="task.opsec_post_blocked === true && task.opsec_post_bypassed === false">
                                 <button class="btn btn{{config['outline-buttons']}}warning btn-sm dropdown-toggle" style="border:0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="card-text">OPSEC BLOCKED (POST)</span></button>
                             </template>
                            <template v-else-if="task.status.includes('error')">
                                <button class="btn btn{{config['outline-buttons']}}danger btn-sm dropdown-toggle"  style="border:0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="card-text">[[task.status]]</span></button>
                            </template>
                            <template v-else-if="task.completed == true || task.status === 'completed'">
                                <button class="btn btn{{config['outline-buttons']}}success btn-sm dropdown-toggle" style="border:0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="card-text">completed</span></button>
                            </template>
                            <template v-else-if="task.status == 'processed'">
                                <button class="btn btn{{config['outline-buttons']}}primary btn-sm dropdown-toggle"  style="border:0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="card-text">[[task.status]]</span></button>
                            </template>
                            <template v-else-if="task.status == 'processing' || task.status == 'cleared'">
                                <button class="btn btn{{config['outline-buttons']}}warning btn-sm dropdown-toggle"  style="border:0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="card-text">[[task.status]]</span></button>
                            </template>

                            <template v-else>
                                <button class="btn btn{{config['outline-buttons']}}info btn-sm dropdown-toggle"  style="border:0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="card-text">[[task.status]]</span></button>
                            </template>
                        <div class="dropdown-menu">
                            <a class="dropdown-item" href="#" data-toggle="tooltip" title="Add comment to task" @click="add_comment(task)"><i class="fas fa-comments"></i> Comment</a>
                            <a class="dropdown-item" href="#" data-toggle="tooltip" title="download raw output" @click="download_raw_output(task.id)"><i class="fas fa-save"></i> Save Output</a>
                            <a class="dropdown-item" href="#" data-toggle="tooltip" title="Copy command to clipboard" @click="copyStringToClipboard(task.command + ' ' + task.original_params)"><i class="far fa-copy" ></i> Copy Command</a>
                            <a class="dropdown-item" href="#" data-toggle="tooltip" title="Toggle application of browser scripts to output of this task" @click="task.use_scripted = !task.use_scripted"><i class="fas fa-code"></i>
                                <template v-if="task.use_scripted">
                                    Disable Browser Script
                                </template>
                                <template v-else>
                                    Enable Browser Script
                                </template>
                            </a>
                            <a class="dropdown-item" href="#" data-toggle="tooltip" title="View Stdout/Stderr for task" @click="view_stdout_stderr(task)"><i class="fas fa-info-circle"></i>View Task Stdout/Stderr</a>
                            <a class="dropdown-item" href="#" data-toggle="tooltip" title="View All Parameters for task" @click="view_all_parameters(task)"><i class="fas fa-info-circle"></i>View All Parameters</a>
                            <template v-if="task.opsec_pre_blocked !== null || task.opsec_post_blocked !== null">
                                <a class="dropdown-item" href="#" data-toggle="tooltip" title="View OPSEC block info" @click="view_opsec_block(task)"><i class="fas fa-info-circle"></i>View OPSEC Message</a>
                            </template>
                            <template v-if="task.opsec_pre_blocked && task.opsec_pre_bypassed === false">
                                <a class="dropdown-item" href="#" data-toggle="tooltip" title="submit bypass request" @click="submit_opsec_bypass_request(task)"><i class="fas fa-info-circle"></i>Submit Bypass Request ([[task.opsec_pre_bypass_role]])</a>
                            </template>
                            <template v-else-if="task.opsec_post_blocked && task.opsec_post_bypassed === false">
                                <a class="dropdown-item" href="#" data-toggle="tooltip" title="submit bypass request" @click="submit_opsec_bypass_request(task)"><i class="fas fa-info-circle"></i>Submit Bypass Request ([[task.opsec_post_bypass_role]])</a>
                            </template>
                        </div>
                    </div>
                     <p :key="'mutedtext' + task.id" class="card-text" style="margin:0;display:inline-block"><small class="text-muted">
                        [[task.operator]]'s task: <a :href="task.href" target="_blank" data-toggle="tooltip" title="View task and output in a separate window">[[task.id]]</a>
                        - at [[toLocalTime(task.timestamp)]]
                     </small></p>
                     <a :key="'commentdots' + task.id" v-if="task.comment !== ''" style="padding-bottom:0" class="btn btn-link" type="button" @click="toggle_comment(task)"><i class="far fa-comment-dots" ></i></a>
                     <br :key="'br' + task.id">
                     <a :key="'toggleoutput' + task.id" class="btn btn-link" type="button" style="border:0;padding:0 0 5px 5px" data-toggle="collapse" :data-target="'#cardbody' + task.id" aria-expanded="false" :aria-controls="'cardbody' + task.id" @click="toggle_arrow(task)"><i :id="'color-arrow' + task.id" class="fas fa-plus fa-xs" data-toggle="tooltip" title="view / hide task output"></i></a>
                        <div style="display:inline-block" :key="'taskcommand' + task.id"><b>&nbsp;[[task.command]]&nbsp;</b></div>
                     <div style="display:inline-block" :key="'taskparams' + task.id">[[task.display_params]]</div>
                    </transition-group>
                </div>
                <div :key="'outputcontent' + task.id" class="collapse" :id="'cardbody' + task.id" >
                    <div :key="'outputcontentcardbody' + task.id" class="response-background card-body shadow" style="padding-top:0;padding-bottom: 0" >
                        <div :key="'showparams' + task.id" v-if="task.show_params">
                            Modified Parameters: &nbsp;[[task.params]]
                        </div>
                        <span :key="'usescripted' + task.id" v-if="task.hasOwnProperty('scripted') && task.use_scripted" v-html="task.scripted"></span>
                        <span v-else :key="'useregular' + task.id" class="card-text" v-for="rsp in task.response" :key="rsp.id" :id="'response' + rsp.id" style="white-space: pre-wrap"><span class="response"><pre>[[rsp.response]]</pre></span></span>
                    </div>
                </div>
                </transition-group>
            </div>
            <table v-if="files.length > 0" class="table table-striped {{config['table-color']}} table-condensed bg-card-body-l2 shadow border border-dark" style="margin-bottom:0;white-space:nowrap;overflow:scroll;">
                    <tr>
                        <th onclick="sort_table(this)">Name</th>
                        <th onclick="sort_table(this)" style="width:7rem">Size</th>
                        <th onclick="sort_table(this)" style="width:14rem">Modify Time</th>
                        <th onclick="sort_table(this)" style="width:20rem">Comment</th>
                        <th style="width:4rem;padding-right:10px">Actions</th>
                    </tr>
                    <tr v-for="f in files">
                        <td style="padding-left:10px">
                            [[f.host]]  -- <i :class="f.is_file ? 'fas fa-file' : 'fas fa-folder'" :style="f.success && !f.is_file ? 'color:#f1d592' : ''"></i>  [[f.full_path]]
                            <template v-if="f.success === true">
                                <i style="color:green" class="fas fa-check"></i>
                            </template>
                            <template v-else-if="f.success === false">
                                <i style="color:red" class="fas fa-exclamation-circle"></i>
                            </template>
                            <template v-if="f['files'].length > 0">
                                <a class="btn btn-md" :href='get_latest_download_path(f["files"])' style="color:green"><i class="fas fa-download fa-lg"></i></a>
                            </template>
                        </td>
                        <td>[[f.size]]</td>
                        <td>[[f.modify_time]]</td>
                        <td>
                            <input type="text" class="form-control" v-model="f.comment" v-on:input="update_file_browser_comment_live(f)">
                        </td>
                        <td>
                            <div class="dropdown" style="display:inline">
                                <button class="btn btn{{config['outline-buttons']}}secondary dropdown-toggle btn-sm" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"  :data-target="f.id + 'action'">
                                <i class="fas fa-edit"> Actions</i>
                              </button>
                              <div class="dropdown-menu" :id="f.id + 'action'">
                                  <button class="dropdown-item"  @click="view_file_data(f)">View Permissions</button>
                                  <button class="dropdown-item"  @click="view_download_history(f)">Download History</button>
                              </div>
                            </div>
                        </td>
                    </tr>
                </table>
                <!-- START POPOVER FOR VIEWING PERMISSIONS -->
                <div id="fileBrowserPermissions" class="modal fade" role="dialog">
                     <div class="modal-dialog modal-xl" role="document">
                         <div class="modal-content">
                             <div class="modal-header bg-dark text-white">
                            <h3>Detailed File Data</h3>
                            <button type="button" class="close" data-dismiss="modal">×</button>
                        </div>
                         <div class="modal-body">
                             <table class="table-striped {{config['table-color']}} table" style="width:100%">
                                <tr>
                                    <th>Key</th>
                                    <th>Value</th>
                                </tr>
                                 <tr v-for="k in Object.keys(file_browser_permissions)">
                                     <td style="padding-left:10px"><pre style="word-wrap:break-word;white-space:pre-wrap">[[ k ]]</pre></td>
                                     <td><pre style="word-wrap:break-word;white-space:pre-wrap">[[ file_browser_permissions[k] ]]</pre></td>
                                 </tr>
                            </table>
                         </div>
                         <div class="modal-footer">
                            <button class="btn btn{{config['outline-buttons']}}success" data-dismiss="modal" aria-hidden="true">Confirm</button>
                        </div>
                         </div>
                     </div>
                </div>
                <!-- END POPOVER FOR VIEWING PERMISSIONS -->
                <!-- START POPOVER FOR VIEWING DOWNLOAD HISTORY -->
                <div id="fileBrowserDownloadHistory" class="modal fade" role="dialog">
                     <div class="modal-dialog modal-xl" role="document">
                         <div class="modal-content">
                             <div class="modal-header bg-dark text-white">
                            <h3>File Download History</h3>
                            <button type="button" class="close" data-dismiss="modal">×</button>
                        </div>
                         <div class="modal-body">
                             <table class="table-striped {{config['table-color']}} table" style="width:100%">
                                <tr>
                                    <th>Download</th>
                                    <th>Timestamp</th>
                                    <th>Comment</th>
                                    <th>Task</th>
                                </tr>
                                 <tr v-for="k in file_browser_history_files">
                                     <td style="width:3rem;text-align:center">
                                         <template v-if="k.deleted">
                                            <i class="fas fa-times-circle" style="color:red"></i>
                                         </template>
                                         <template v-else>
                                             <a class="btn btn-md" :href='get_file_download_path(k)' style="color:green"><i class="fas fa-download fa-lg"></i></a>
                                         </template>
                                    </td>
                                     <td style="width:12rem">[[ toLocalTime(k['timestamp']) ]]</td>
                                     <td>
                                         <input type="text" class="form-control" v-model="k['comment']" v-on:input="update_comment_live(k['task'], k['comment'])">
                                     </td>
                                     <td style="width:5rem;text-align:center">
                                         <a :href="get_task_view_path(k)" target="_blank">[[ k['task'] ]]</a>
                                     </td>
                                 </tr>
                            </table>
                         </div>
                         <div class="modal-footer">
                            <button class="btn btn{{config['outline-buttons']}}success" data-dismiss="modal" aria-hidden="true">Confirm</button>
                        </div>
                         </div>
                     </div>
                </div>
                <!-- END POPOVER FOR VIEWING DOWNLOAD HISTORY -->
            <table v-if="event_messages.length > 0" class="table table-striped {{config['table-color']}} table-condensed bg-card-body-l2 shadow border border-dark" style="margin-bottom:0;white-space:nowrap;overflow:scroll;">
                <tr>
                    <th>Time</th>
                    <th>Operator</th>
                    <th>Message</th>
                </tr>
                <tr v-for="e in event_messages">
                    <td style="padding-left:10px"><pre>[[toLocalTime(e.timestamp)]]</pre></td>
                    <td>
                        <template v-if="e.operator !== 'null'">
                            [[e.operator]]
                        </template>
                        <template v-else>
                            Mythic
                            <template v-if="e.level === 'warning' && e.resolved">
                                <br>(Resolved)
                            </template>
                            <template v-else-if="e.level === 'warning' && !e.resolved" style="color:red">
                                <br>(Unresolved)
                            </template>
                        </template>
                    </td>
                    <td>
                        <template v-if="e.deleted">
                            (deleted) <pre>[[e.message]]</pre>
                        </template>
                        <template v-else>
                            <pre>[[e.message]]</pre>
                        </template>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="card-footer bg-card-footer">
        <div style="display:inline-block">
            <button class="btn btn-md btn{{config['outline-buttons']}}info" @click="export_page()"><i class="fas fa-download fa-lg"></i> Page</button>
            <button class="btn btn-md btn{{config['outline-buttons']}}info" @click="export_search()"><i class="fas fa-download fa-lg"></i> Search</button>
            <button type="button" :disabled="current_page <= 1"  class="btn btn-light" data-toggle="tooltip" title="Go back one page" @click="get_page(current_page-1)"><i class="fas fa-backward"></i></button>&nbsp;
            <template v-if="current_page > 1">
                <button type="button" class="btn btn-light" data-toggle="tooltip" title="Go to first page" @click="get_page(1)">1</button>
            </template>
            <template v-if="current_page > 2">&nbsp;<font size="5" class="text-white">...</font>&nbsp;</template>
            <template v-if="current_page -2 > 1">
                <button type="button" class="btn btn-light" @click="get_page(current_page -2)">[[current_page - 2]]</button>
            </template>
            <template v-if="current_page -1 > 1">
                <button type="button" class="btn btn-light" @click="get_page(current_page -1)">[[current_page - 1]]</button>
            </template>
            <button type="button" disabled class=" btn btn-light" data-toggle="tooltip" title="Current page">[[current_page]] </button>
            <template v-if="current_page + 1 < Math.ceil(total_count/page_size)">
                <button type="button" class="btn btn-light" @click="get_page(current_page + 1)">[[current_page + 1]]</button>
            </template>
            <template v-if="current_page + 2 < Math.ceil(total_count/page_size)">
                <button type="button" class="btn btn-light" @click="get_page(current_page +2)">[[current_page + 2]]</button>
            </template>
            <template v-if="current_page < Math.ceil(total_count/page_size) -1">
                &nbsp;<font size="5" class="text-white">...</font>&nbsp;
            </template>
            <template v-if="current_page != Math.ceil(total_count/page_size) && total_count > 0">
                <button type="button" class="btn btn-light" data-toggle="tooltip" title="Go to last page" @click="get_page(Math.ceil(total_count/page_size))">[[Math.ceil(total_count/page_size)]]</button>
            </template>

            <button type="button" class="btn btn-light" :disabled="current_page == Math.ceil(total_count/page_size) || Math.ceil(total_count/page_size) == 0" data-toggle="tooltip" title="Go forward one page" @click="get_page(current_page + 1)"><i class="fa fa-forward"></i></button>
            &nbsp;<font size="4" class="text-white">Total Count: [[total_count]]</font>
        </div>

        <div style="display:inline-block; float:right">
            <span class="input-group">
                <input class="form-control" type="number" size="5em" :placeholder="page_size" id="page_size" @keyup.enter="get_new_page_size()">
                <div class="input-group-append input-group-btn" ><button  type="button" class="btn btn{{config['outline-buttons']}}success" @click="get_new_page_size()">Edit page size</button></div>
            </span>
        </div>

    </div>
</div>
<!-- THIS IS OUR MODAL FOR ADDING A COMMENT -->
<div class="modal fade" id="addCommentModal" role="dialog" >
    <div class="modal-dialog modal-xl" role="document">
        <div class="modal-content">
            <div class="modal-header bg-dark text-white">
                <h3>Add / Edit Comment</h3>
                <button type="button" class="close" data-dismiss="modal">×</button>
            </div>
            Shift+Enter to add a new line, Enter to submit
              <div class="modal-body">
                <textarea class="form-control" rows="25" style="width:100%" id="addCommentTextArea"></textarea>
              </div>
              <div class="modal-footer">
                <button class="btn btn{{config['outline-buttons']}}success" data-dismiss="modal" id="addCommentSubmit" aria-hidden="true">Submit</button>
              </div>
        </div>
    </div>
</div>
<!-- END MODAL FOR ADDING A COMMENT -->

{% endblock %}

{% block scripts %}
{% include "search.js" %}
{% endblock %}

{% block body_config %}

{% endblock %}